package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.lsy.weizhi2.utils.RegexUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class Setting_Phone extends MyBaseActivity {
    private Context context;
    private MyTitleView phone_title;
    private EditText new_phone,phone_code_et;
    private TextView phone_code_tv;
    private Button phone_btn;
    private int xx;
    private String user_phone;
    private Handler handler=new Handler();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            phone_code_tv.setText(l / 1000 + "秒后重发");
        }

        @Override
        public void onFinish() {
            phone_code_tv.setEnabled(true);
            phone_code_tv.setClickable(true);
            phone_code_tv.setText("获取验证码");
            phone_code_tv.setTextColor(context.getResources().getColor(R.color.clicked_ok));
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting__phone);
        context=Setting_Phone.this;
        findView();
    }

    private void findView() {
        phone_title= (MyTitleView) findViewById(R.id.phone_title);
        new_phone= (EditText) findViewById(R.id.new_phone);
        phone_code_et= (EditText) findViewById(R.id.phone_code_et);
        phone_code_tv= (TextView) findViewById(R.id.phone_code_tv);
        phone_btn= (Button) findViewById(R.id.phone_btn);
        phone_code_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user_phone=new_phone.getText().toString().trim();
                if (RegexUtils.checkMobile(user_phone)) {
                    Getcode(user_phone);
                } else {
                    MyMethod.ShowDialog(context, "请输入正确手机号码");
                }
            }
        });
        phone_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code=phone_code_et.getText().toString().trim();
                Updata(user_phone,code);
            }
        });
    }

    private void Getcode(String phone) {
        MyOkHttpUtils.getInstance(context).doPost(Constants.SEND_SMS + phone, null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if (message != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if (result) {
                                    timer.start();
                                }else{
                                    String msg=jsonObject.getString("message");
                                    if(msg.equals("错误的认证Token")){
                                        MyMethod.ShowDialogLogin(context,LoginActivty.class);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    private void Updata(String newPhone,String verifyCode){
        Map<String,String> map=new HashMap<>();
        map.put("newPhone",newPhone);
        map.put("verifyCode",verifyCode);
        MyOkHttpUtils.getInstance(context).doPut(Constants.RESET_PHONE,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("SettingPhone","message==="+message);
                        if(message!=null){
                            try {
                                JSONObject json=new JSONObject(message);
                                boolean result=json.getBoolean("result");
                                if(result){
                                    MyMethod.showToast(context,"修改成功");
                                    finish();
                                }else{
                                    String msg=json.getString("message");
                                    if(msg.equals("错误的认证Token")){
                                        MyMethod.ShowDialogLogin(context,LoginActivty.class);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = phone_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(phone_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                phone_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
    }
}
